package view;

import service.impl.MovieServiceImpl;
import service.impl.ScheduleServiceImpl;
import util.InputUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class ManagerView {
    private final MovieServiceImpl movieService = new MovieServiceImpl();
    private final ScheduleServiceImpl scheduleService = new ScheduleServiceImpl();

    public void start() {
        while (true) {
            System.out.println("\n=== 经理菜单 ===");
            System.out.println("1. 影片管理");
            System.out.println("2. 排片管理");
            System.out.println("3. 查看销售数据");
            System.out.println("0. 退出登录");
            System.out.print("请选择：");
            switch (InputUtil.nextInt()) {
                case 1:
                    movieManagement();
                    break;
                case 2:
                    scheduleManagement();
                    break;
                case 3:
                    salesDataManagement();
                    break;
                case 0:
                    movieService.shutdown();
                    scheduleService.shutdown();
                    return;
                default:
                    System.out.println("输入错误！");
            }
        }
    }

    private void movieManagement() {
        while (true) {
            System.out.println("\n=== 影片管理 ===");
            System.out.println("1. 列出所有影片");
            System.out.println("2. 添加影片");
            System.out.println("3. 修改影片");
            System.out.println("4. 删除影片");
            System.out.println("5. 查询影片");
            System.out.println("0. 返回上一级");
            System.out.print("请选择：");
            switch (InputUtil.nextInt()) {
                case 1:
                    List<entity.Movie> movies = movieService.findAll();
                    movies.forEach(System.out::println);
                    break;
                case 2:
                    System.out.print("影片ID（自定义唯一ID）：");
                    String movieId = InputUtil.nextLine();
                    System.out.print("片名：");
                    String name = InputUtil.nextLine();
                    System.out.print("导演：");
                    String director = InputUtil.nextLine();
                    System.out.print("主演：");
                    String actors = InputUtil.nextLine();
                    System.out.print("剧情简介：");
                    String description = InputUtil.nextLine();
                    System.out.print("时长（分钟）：");
                    int duration = InputUtil.nextInt();
                    System.out.print("评分（0-10）：");
                    double rating = InputUtil.nextDouble();
                    movieService.add(new entity.Movie(movieId, name, director, actors, description, duration, rating));
                    System.out.println("影片添加成功！");
                    break;
                case 3:
                    System.out.print("输入影片ID修改：");
                    String id = InputUtil.nextLine();
                    // 更新逻辑省略，需要你自己完善
                    break;
                case 4:
                    System.out.print("输入影片ID删除：");
                    String movieIdToDelete = InputUtil.nextLine();
                    if (movieService.delete(movieIdToDelete)) {
                        System.out.println("删除成功！");
                    } else {
                        System.out.println("未找到影片或影片正在排片！");
                    }
                    break;
                case 5:
                    System.out.print("输入查询关键词：");
                    String keyword = InputUtil.nextLine();
                    movieService.searchByKeyword(keyword).forEach(System.out::println);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误！");
            }
        }
    }

    private void scheduleManagement() {
        while (true) {
            System.out.println("\n=== 排片管理 ===");
            System.out.println("1. 增加场次");
            System.out.println("2. 修改场次");
            System.out.println("3. 删除场次");
            System.out.println("4. 列出场次");
            System.out.println("5. 自动排片");
            System.out.println("0. 返回上一级");
            System.out.print("请选择：");
            switch (InputUtil.nextInt()) {
                case 1:
                    List<entity.Movie> availableMovies = movieService.findAll();
                    System.out.println("可用影片：");
                    availableMovies.forEach(movie -> System.out.println(movie.getId() + ": " + movie.getName()));
                    System.out.print("影片ID：");
                    String movieId = InputUtil.nextLine();

                    String[] halls = {"黄金厅", "白银厅", "翡翠厅", "钻石厅", "宝石厅"};
                    System.out.println("可用放映厅：");
                    for (int i = 0; i < halls.length; i++) {
                        System.out.println((i + 1) + ". " + halls[i]);
                    }
                    System.out.print("选择放映厅（输入1-5）：");
                    int hallIndex = InputUtil.nextInt() - 1;
                    if (hallIndex < 0 || hallIndex >= halls.length) {
                        System.out.println("选择无效！");
                        break;
                    }
                    String hallName = halls[hallIndex];

                    System.out.print("场次ID（自定义唯一ID）：");
                    String scheduleId = InputUtil.nextLine();

                    System.out.print("开始时间（yyyy-MM-dd HH:mm）：例如 2025-05-04 19:00 ");
                    String datetimeStr = InputUtil.nextLine();
                    try {
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
                        LocalDateTime startTime = LocalDateTime.parse(datetimeStr, formatter);
                        System.out.print("票价：");
                        double price = InputUtil.nextDouble();
                        scheduleService.add(new entity.Schedule(scheduleId, movieId, hallName, startTime, price));
                        System.out.println("场次添加成功！");
                    } catch (Exception e) {
                        System.out.println("日期时间格式错误，请使用 yyyy-MM-dd HH:mm 格式！");
                    }
                    break;
                case 2:
                    System.out.print("输入场次ID修改：");
                    String sid = InputUtil.nextLine();
                    // 更新逻辑省略，需要你自己完善
                    break;
                case 3:
                    System.out.print("输入场次ID删除：");
                    String scheduleIdToDelete = InputUtil.nextLine();
                    if (scheduleService.delete(scheduleIdToDelete)) {
                        System.out.println("删除成功！");
                    } else {
                        System.out.println("未找到场次或场次已售出票！");
                    }
                    break;
                case 4:
                    scheduleService.findAll().forEach(System.out::println);
                    break;
                case 5:
                    scheduleService.autoSchedule();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误！");
            }
        }
    }

    private void salesDataManagement() {
        while (true) {
            System.out.println("\n=== 销售数据 ===");
            System.out.println("1. 当日销售数据");
            System.out.println("2. 当周销售数据");
            System.out.println("3. 当月销售数据");
            System.out.println("0. 返回上一级");
            System.out.print("请选择：");
            switch (InputUtil.nextInt()) {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误！");
            }
        }
    }
}